package ui.popup 
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import ui.button.GCButton;
	import ui.textfield.GCTextField;
	
	/**
	 * ...
	 * @author Roy
	 */
	public class GCSummaryPopup extends Sprite 
	{
		
		private static const TEXT_AREA_WIDTH:uint = 400;
		private static const X_OFFSET:uint = 20;
		
		private var _welcomeBackLabel:GCTextField;
		
		private var _scrollableTextArea:Sprite;
		private var _salesLabel:GCTextField;
		private var _salesTextArea:GCTextField;
		private var _gatheredLabel:GCTextField;
		private var _gatheredResourceArea:GCTextField;
		private var _questsLabel:GCTextField;
		private var _questsTextArea:GCTextField;
		
		private var _mask:Shape;
		
		private var _salesContent:String;
		private var _resourceContent:String;
		private var _questContent:String;
		
		private var _confirmButton:GCButton;
		private var _scrollUp:GCButton;
		private var _scrollDown:GCButton;
		
		private var _endingFunction:Function;
		
		public function GCSummaryPopup(sales:String, resourcesCollected:String, quests:String, endFunc:Function) 
		{
			_salesContent = sales;
			_resourceContent = resourcesCollected;
			_questContent = quests;
			
			_endingFunction = endFunc;
			
			_mask = new Shape();
			
			_scrollableTextArea = new Sprite();
			_welcomeBackLabel = new GCTextField();
			_salesLabel = new GCTextField();
			_salesTextArea = new GCTextField();
			_gatheredLabel = new GCTextField();
			_gatheredResourceArea = new GCTextField();
			_questsLabel = new GCTextField();
			_questsTextArea = new GCTextField();
			
			_confirmButton = new GCButton(onConfirmClick);
			_scrollUp = new GCButton(onScrollUp);
			_scrollDown = new GCButton(onScrollDown);
			
			init();
		}
		
		private function init():void
		{
			var bg:Bitmap = GCResources.getCanvasBackground(300, 400);
			this.addChild(bg);
			
			_welcomeBackLabel.text = "Welcome back! While you were away:";
			_welcomeBackLabel.setDimensions(bg.width, 30);
			this.addChild(_welcomeBackLabel);
			
			_salesLabel.defaultTextFormat = GCTextField.SUMMARY_LABEL_FORMAT;
			_salesLabel.text = "Goods Sold:";
			//_salesLabel.x = X_OFFSET;
			_salesLabel.setDimensions(bg.width, 30);
			_salesTextArea.autoSize = TextFieldAutoSize.LEFT;
			_salesTextArea.setDimensions(bg.width, 50);
			_salesTextArea.setStyle(GCTextField.TEXT_TYPE_LEFT);
			_salesTextArea.text = _salesContent;
			_salesTextArea.setXY(X_OFFSET, _salesLabel.height + _salesLabel.y);
			
			_gatheredLabel.defaultTextFormat = GCTextField.SUMMARY_LABEL_FORMAT;
			_gatheredLabel.text = "Resources Collected:";
			//_gatheredLabel.x = X_OFFSET;
			_gatheredLabel.setDimensions(bg.width, 30);
			_gatheredLabel.setXY(0, _salesTextArea.y + _salesTextArea.height);
			_gatheredResourceArea.autoSize = TextFieldAutoSize.CENTER;
			_gatheredResourceArea.setDimensions(bg.width / 2, 50);
			_gatheredResourceArea.setStyle(GCTextField.TEXT_TYPE_LEFT);
			_gatheredResourceArea.text = _resourceContent;
			_gatheredResourceArea.setXY(X_OFFSET, _gatheredLabel.height + _gatheredLabel.y);
			
			_questsLabel.defaultTextFormat = GCTextField.SUMMARY_LABEL_FORMAT;
			//_questsLabel.x = X_OFFSET;
			_questsLabel.text = "Orders in Progress:";
			_questsLabel.setDimensions(bg.width, 30);
			_questsLabel.setXY(0, _gatheredResourceArea.y + _gatheredResourceArea.height);
			_questsTextArea.autoSize = TextFieldAutoSize.CENTER;
			_questsTextArea.setStyle(GCTextField.TEXT_TYPE_LEFT);
			_questsTextArea.text = _questContent;
			_questsTextArea.setDimensions(bg.width, 50);
			_questsTextArea.setXY(X_OFFSET, _questsLabel.height + _questsLabel.y);
			
			_scrollableTextArea.x = X_OFFSET;
			_scrollableTextArea.y = _welcomeBackLabel.y + _welcomeBackLabel.height;
			
			// Sales since last login
			_scrollableTextArea.addChild(_salesLabel);
			_scrollableTextArea.addChild(_salesTextArea);
			
			// Resources gathered since last login
			_scrollableTextArea.addChild(_gatheredLabel);
			_scrollableTextArea.addChild(_gatheredResourceArea);
			
			// Orders still in progress
			_scrollableTextArea.addChild(_questsLabel);
			_scrollableTextArea.addChild(_questsTextArea);
			this.addChild(_scrollableTextArea);
			
			_mask.graphics.beginFill(GCConstants.COLOR_WHITE);
			_mask.graphics.drawRoundRect(0, 0, this.width - 50, 300, 5);
			_mask.x = _scrollableTextArea.x;
			_mask.y = _scrollableTextArea.y;
			this.addChild(_mask);
			_scrollableTextArea.mask = _mask;
			
			var scrollableTextBg:Shape = new Shape();
			scrollableTextBg.graphics.beginFill(GCConstants.COLOR_GOLD, 0.6);
			scrollableTextBg.graphics.drawRoundRect(_mask.x, _mask.y, _mask.width - X_OFFSET, _mask.height, 20);
			scrollableTextBg.graphics.endFill();
			this.addChildAt(scrollableTextBg, 1);
			
			_scrollUp.loadGraphic(GCResources.up_arrow, 25, 35);
			_scrollDown.loadGraphic(GCResources.down_arrow, 25, 35);
			_scrollUp.x = _mask.x + _mask.width - X_OFFSET * 2;
			_scrollUp.y = _mask.y;
			_scrollDown.x = _scrollUp.x;
			_scrollDown.y = _mask.y + _mask.height - _scrollDown.height;
			this.addChild(_scrollUp);
			this.addChild(_scrollDown);
			
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("OK");
			_confirmButton.x = bg.width / 2 - _confirmButton.width / 2;
			_confirmButton.y = bg.height - _confirmButton.height;
			this.addChild(_confirmButton);
			
		}
		
		private function onConfirmClick(event:MouseEvent):void
		{
			_confirmButton.removeEventListener(MouseEvent.CLICK, onConfirmClick);
			_scrollUp.removeEventListener(MouseEvent.CLICK, onScrollUp);
			_scrollDown.removeEventListener(MouseEvent.CLICK, onScrollDown);
			
			_endingFunction.call();
		}
		
		private function onScrollUp(event:MouseEvent):void
		{
			_scrollableTextArea.y += 20;
			
			if (_scrollableTextArea.y > _mask.y)
			{
				_scrollableTextArea.y = _mask.y;
			}
			
		}
		
		private function onScrollDown(event:MouseEvent):void
		{
			if (_scrollableTextArea.y + _scrollableTextArea.height > _mask.y + _mask.height)
			{
				_scrollableTextArea.y -= 20;
			}
		}
	}

}